package com.foreverwin.mes.warehouse.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.foreverwin.mes.warehouse.constant.Constants;
import com.foreverwin.mes.warehouse.mapper.YieldConfirmMapper;
import com.foreverwin.mes.warehouse.model.YieldConfirm;
import com.foreverwin.mes.warehouse.service.YieldConfirmService;
import com.foreverwin.mes.warehouse.vo.YieldConfirmVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 成品入库确认 服务实现类
 * </p>
 * 将生产提交的入库信息进行确认，确认后库存从线边仓调拨至成品仓
 *
 * @author Ervin Chen
 * @since 2020-02-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class YieldConfirmServiceImpl extends ServiceImpl<YieldConfirmMapper, YieldConfirm> implements YieldConfirmService {


    @Autowired
    private YieldConfirmMapper yieldConfirmMapper;

    @Override
    public Page<YieldConfirm> selectPage(FrontPage<YieldConfirm> frontPage, YieldConfirm yieldConfirm) {
        EntityWrapper<YieldConfirm> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(yieldConfirm);
        return super.selectPage(frontPage.getPagePlus(), queryWrapper);
    }

    @Override
    public List<YieldConfirm> selectList(YieldConfirm yieldConfirm) {
        EntityWrapper<YieldConfirm> queryWrapper = new EntityWrapper<>();
        queryWrapper.setEntity(yieldConfirm);
        return super.selectList(queryWrapper);
    }

    @Override
    public List<YieldConfirmVo> selectYieldConfirmList(YieldConfirm yieldConfirm) {
        String site = CommonMethods.getSite();
        yieldConfirm.setSite(site);
        yieldConfirm.setIsConfirmed(Constants.NOT_CONFIRMED);
        EntityWrapper<YieldConfirm> entityWrapper = new EntityWrapper<>();
        entityWrapper.setEntity(yieldConfirm);
        entityWrapper.orderAsc(Arrays.asList("created_date_time", "inventory_id"));

        Locale locale = LocaleContextHolder.getLocale();
        return yieldConfirmMapper.selectYieldConfirmList(locale.getLanguage(), entityWrapper);
    }

    @Override
    public boolean insertOrUpdate(List<YieldConfirm> yieldConfirms) {
        String userBo = CommonMethods.getUserBo();
        Date now = new Date();

        for (YieldConfirm yieldConfirm : yieldConfirms) {

            //入库信息进行确认
            yieldConfirm.setIsConfirmed(Constants.CONFIRMED);
            yieldConfirm.setUpdateUserBo(userBo);
            yieldConfirm.setUpdatedDateTime(now);
            this.updateById(yieldConfirm);

            //成品库存从线边仓调拨至成品仓

        }
        return true;
    }


}